from typing import Any, Optional
from pydantic import BaseModel, Field


class DeviceItem(BaseModel):
    no: str = Field(title="设备编号")
    name: str = Field(title="设备名称")


class ScriptSchema(BaseModel):
    name: str = Field(title="脚本名称")
    args: int = Field(title="脚本参数类型：1-单台设备 2-网关信息")
    mode: str = Field(default="polling", title="采集方式", description="polling-轮询 interval-定时触发 advanced-高级定时设置")
    content: str = Field(title="脚本内容")
    # 定时触发及高级定时公用的三个参数
    hour: Optional[int | str] = Field(default="", title="每天第x小时触发或x小时触发一次", description="取值范围0-23")
    minute: Optional[int | str] = Field(default="", title="每小时第x分钟触发或x分钟触发一次", description="取值范围0-59")
    second: Optional[int | str] = Field(default="", title="每分钟第x秒触发或x秒触发一次", description="取值范围0-59")
    # 高级定时的额外参数
    year: Optional[int | str] = Field(default="", title="x年触发", description="4位数字，支持cron算数表达式，下同")
    month: Optional[int | str] = Field(default="", title="x月触发", description="取值范围1-12")
    day: Optional[int | str] = Field(default="", title="x日触发", description="取值范围1-31")
    week: Optional[int | str] = Field(default="", title="第x周触发", description="取值范围1-53")
    day_of_week: Optional[int | str] = Field(default="", title="每周第x天触发", description="取值范围0-6，或周的简写")

    devices: list[str] = Field(default=[], title="脚本适用的设备列表")
    mqttClients: list[str] = Field(default=[], title="脚本结果输出的客户端")


class ScriptOutSchema(ScriptSchema):
    devices: list[DeviceItem] = Field(title="脚本适用的设备列表")


class ScriptRunSchema(BaseModel):
    content: str = Field(title="脚本程序本体")
    clients: list[str] = Field(title="接收脚本返回值的客户端")


class ScriptTestSchema(BaseModel):
    device: str = Field(title="参与测试的设备")
    client: Optional[str] = Field(default="", title="参与测试的客户端", description="为空时运行结果将直接以字符串形式返回")
    script: str = Field(title="测试脚本")


class ScriptTestResult(BaseModel):
    code: int = Field(default=200, title="脚本运行结果状态码")
    msg: str | dict = Field(default="执行完毕，未发现异常", title="脚本执行结果输出")

